package com.example.security.funtion;

import com.example.core.utils.StringUtils;
import com.example.core.utils.UserUtils;
import com.example.sys.module.po.SysModule;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;
import org.springframework.stereotype.Service;

import java.util.Map;

/***
 *
 * 判断用户对于模块的删除角色
 */
@Service
public class DeleteRoleFunction extends SysAbstractFunction {

    @Override
    public AviatorObject call(Map<String, Object> env) {
        if (env instanceof FunctionContext) {
            FunctionContext context = (FunctionContext) env;
            String poClassName = context.getBaseService().getPoClass().getName();
            if (StringUtils.isNotEmpty(poClassName)) {
                SysModule sysJSONModule = getSysModuleService().getModuleByPo(poClassName);
                if (!UserUtils.hasRole(sysJSONModule.getDelRole())) {
                    return AviatorBoolean.FALSE;
                }
            }
        }
        return AviatorBoolean.TRUE;
    }
}
